﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainView.Model
{
    class PlcCon
    {
        string ip;     
        public string Ip { get => ip; set => ip = value; }
        //Plc S7200SMART = new Plc(CpuType.S71200, Ip, 0, 1);

        public bool isConnect()
        {
            return true;
        }
        public byte[] askOutbyte(string ip,int addr)
        {
            Plc s7200Smart = new Plc(CpuType.S71200,ip,0,1);
            byte[] sta = new byte[1];
            try
            {
                s7200Smart.Open();
                sta = s7200Smart.ReadBytes(DataType.Output, 0, addr, 1);
                s7200Smart.Close();
                return sta;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return sta;
            }
        }
        public ErrorCode setOutbyte(string ip, int addr,byte[] value)
        {
            Plc s7200Smart = new Plc(CpuType.S71200, ip, 0, 1);
            try
            {
                s7200Smart.Open();
                ErrorCode errorCode = s7200Smart.WriteBytes(DataType.Memory, 0, addr, value);
                s7200Smart.Close();
                return errorCode;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
           
        }
    }
}
